<?php

class ReviewController extends Controller
{
    
    public function actionAdd()
    {
	if (Yii::app()->getRequest()->getIsPostRequest()) {
	    $data = Yii::app()->getRequest()->getParam('review');
	    $errors = false;
	    if (empty($data)) {
		$errors[] = "Данные не переданы";
	    }
	    $data['title'] = PurifierManager::getInstance()->filter($data['title']);
	    $data['content'] = PurifierManager::getInstance()->filter($data['content']);

	    if (empty($data['title'])) {
		$errors[] = "Тема не указана";
	    }
	    if (empty($data['content'])) {
		$errors[] = "Описание не указано";
	    }
	    if (!$errors) {
		if (!key_exists('id', $data)) {
		    $data['url'] = Translit::encode($data['title']);
		}
		$data['add_date'] = date('Y-m-d H:i:s');
		$data['user_id'] = Yii::app()->user->getId();

		$features = Yii::app()->getRequest()->getParam('features');

		$data['has_map'] = 0;
		$data['has_image'] = 0;
		$data['has_video'] = 0;
		if (!empty($features)) {
		    $keys = array_keys($features);
		    foreach ($keys as $key) {
			$data['has_' . $key] = 1;
		    }
		}

		if (isset($data['id'])) {
		    $model = FactoryService::get('review')->get($data['id'])->setDbData((object) $data);
		}
		else {
		    $model = new ReviewModel((object) $data);
		}

		if (FactoryService::get('review')->save($model)) {
		    if (!empty($features)) {
			FactoryService::get('review')->compareFeatures($model, $features);
		    }
		    Yii::app()->user->setFlash('saveReview#', 'Ваша запись сохранена');
		    Yii::app()->getRequest()->redirect($this->createUrl('review/view', array(
				'id' => $model->getId(),
				'url' => $model->getDbData('url')
			    )));
		}
	    }
	    else {
		Yii::app()->user->setFlash('reviewErrors', implode("<br />", $errors));
	    }
	}
	$this->breadcrumbs = array(
	    'Добавление отзыва'
	);
	$this->render('add');
    }

    public function actionEdit()
    {
	$this->layout = 'layout';
	if (Yii::app()->user->getIsGuest()) {
	    throw new CHttpException(404, 'Нету записи');
	}
	$id = Yii::app()->request->getParam('id', false);
	if (!$id) {
	    throw new CHttpException(404, 'Нету записи');
	}
	$model = FactoryService::get('review')->get($id);
	if (!$model) {
	    throw new CHttpException(404, 'Нету записи');
	}

	if ($model->getDbData('user_id') != Yii::app()->user->getId()) {
	    throw new CHttpException(404, 'Нету записи');
	}
	FactoryService::get('review')->getExtendedData($model);
	$this->breadcrumbs = array(
	    'Редактирование отзыва'
	);
	$this->render('edit', array('model' => $model));
    }

    public function actionList()
    {
	$page = Yii::app()->request->getParam('page', 1);

	$pager = new CPagination(FactoryService::get('review')->getCount());
	$pager->setPageSize(Yii::app()->params['perPage']);

	$modelList = FactoryService::get('review')->getReviewList(array(), $page, $pager->getLimit());
	$this->breadcrumbs = array(
	    'Новые отзывы'
	);
	$this->render('list', array('models' => $modelList, 'pager' => $pager, 'page' => $page));
    }

    public function actionIndex()
    {
	//Z::flush();
//	$this->render('index', array(
//	    'lastReviews' => FactoryService::get('review')->getReviewList(),
//	    'positiveOpinions' => FactoryService::get('opinion')->getLastPositiveOpinions(),
//	    'negativeOpinions' => FactoryService::get('opinion')->getLastNegativeOpinions(),
//	));
	$this->render('index');
    }

    public function actionView()
    {
	$id = Yii::app()->request->getParam('id', false);
	if (!$id) {
	    throw new CHttpException(404, 'Нету записи');
	}
	$model = FactoryService::get('review')->get($id);
	if (!$model) {
	    throw new CHttpException(404, 'Нету записи');
	}
	FactoryService::get('review')->getExtendedData($model);
	$crumbs = array();
	if ($model->getCategory()) {
	    $crumbs[$model->getCategory()->title] = array('category/view', 'name' => $model->getCategory()->name);
	}
	$crumbs[] = $model->getDbData('title');
	$this->breadcrumbs = $crumbs;

	SeoManager::getInstance()->set(array(
	    'title' => $model->title,
	    'description' => substr($model->content, 0, 250),
	    'keywords' => substr($model->content, 0, 250)	    
	));

	$this->render('view', array(
	    'model' => $model	    
	));
    }

}
